#include <stdio.h>

#define STDIN
//#define DEBUG

#define MAX 9999999

int P, V;
int c[31][301];/*c[i][j]表示第i个邮局在第j个村庄上*/
//最终结果为 min{d[V][i] + dist[i:P]; 1<= i <= p}
int d[301];//d[i][j]表示村庄i,j之间位置

#ifdef DEBUG
void dump()
{
    int i, j;
    for (i = 0; i <= P; ++i)
    {
        for (j = 0; j <= V; ++j)
        {
            printf("%d\t", c[i][j]);
        }
        printf("\n");
    }
}
#endif

int dist(int left, int right)
{
    int i;
    int min_dist = 0;
    int d1, d2;

    for (i = left; i <= right; ++i)
    {
        d1 = d[i] - d[left-1];
        d2 = d[right+1] - d[i];
        min_dist += (d1 < d2 ? d1 : d2);
    }
    return min_dist;
}

void find_min_dist()
{
    int i, j, k;


    //对d进行处理，虚构一个村庄， 并在那里建一个虚拟邮局，
    //使d[0] = -(d[V] +1);
    //这样当只有一个真实邮局和一个虚拟邮局时，所有村庄都会只会选择真实的邮局
    d[0] = -(d[V] + 1);
#ifdef DEBUG
    for (i = 0; i <= V; ++i)
        printf("%d\t", d[i]);
    printf("\n");
#endif

    for (i = 1; i <= V; i++)
    {
        for (j = 1; j < i; ++j)
            c[1][i] += (d[i] - d[j]);
    }
#ifdef DEBUG
    dump();
#endif
    
    int pi, vi;
    for (pi = 2; pi <= P; ++pi)
    {
        for (vi = pi+1; vi <= V; ++vi)
        {
            int min = MAX;
            int tmp;
            
            for (k = pi-1; k <= vi-1; k++)
            {
                tmp = (c[pi-1][k] + dist(k+1, vi-1)); 
                if (tmp < min)
                    min = tmp;
                
            }
            c[pi][vi] = min;
        }
    }

#ifdef DEBUG
    dump();
#endif
    int min = MAX;
    for (i = P; i <= V; ++i)
    {
        int left = 0;
        for (j = i+1; j <= V; ++j)
            left += (d[j] - d[i]);
        if (min > c[P][i] + left)
            min = c[P][i] + left;
    }
    printf("%d\n", min);
}

int main()
{
#ifdef STDIN
    freopen("bin/1160.in", "r", stdin);
#endif


    scanf("%d %d", &V, &P);

    int i;
    for (i = 1; i <= V; ++i)
        scanf("%d",&d[i]); 

    find_min_dist();

    return 0;
}
